package com.p7.framework.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.alibaba.fastjson.JSON;
import com.p7.framework.common.validator.LocaleService;
import com.p7.framework.common.validator.ValidatorUtils;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.p7.framework.common.ResultBean;
import com.p7.framework.common.exception.RequestParamException;
import com.p7.framework.entity.Person;

/**
 * @author p7+
 * @ClassName: BindingResultController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2018年10月18日 下午5:38:32
 * @Copyright: 2018
 **/
@Controller
@RequestMapping("/bindingResult")
public class BindingResultController {

    @Resource
    private LocaleService localeService;

    @ResponseBody
    @RequestMapping("/test")
    public ResultBean testBindingResult(@NotBlank String name, BindingResult result) {

        if (result.hasErrors()) {
            System.out.println(result.getFieldError("name").getDefaultMessage());
        }

        System.out.println(name);

        return new ResultBean();
    }

    @ResponseBody
    @RequestMapping("/test1")
    public ResultBean testBindingResult2(@Valid Person person, BindingResult result) {

        ResultBean resultBean = ValidatorUtils.beanValid(result, localeService);
        if (resultBean != null) {
            return resultBean;
        }
        return new ResultBean();
    }

}
